﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoVanHocTapData.Interface;
using CoVanHocTapData.Entities;

namespace CoVanHocTapData.Concretes
{
    public class RangBuocMonRepository:IRangBuocMonRepository
    {
        DangKyPhuEntities entities;
        public RangBuocMonRepository()
        {
            entities = new DangKyPhuEntities();
        }
        //0: la mon tien quyet; 1 la mon hoc truoc; 2: mh song hanh
        /// <summary>
        /// lay mon rang buoc truoc cua monhocID
        /// </summary>
        /// <param name="monhocID"></param>
        /// <param name="nganhID"></param>
        /// <param name="strSongHanh"></param>
        /// <param name="strTienQuyet"></param>
        /// <param name="strTruoc"></param>
        /// <returns></returns>
        public bool LayMonRangBuoc(string monhocID, string nganhID, out string strSongHanh, out string strTienQuyet, out string strTruoc)
        {
            var rangbuocList = from m in entities.RangBuocMons
                               where m.MonHocIDSau.Equals(monhocID)
                               && m.NganhID.Equals(nganhID)
                               select m;
            //0: la mon tien quyet; 1 la mon hoc truoc; 2: mh song hanh
            strSongHanh = "";
            strTienQuyet = "";
            strTruoc = "";
            string tienQuyetList = "";
            string truocList = "";
            string songHanhList = "";

            foreach (var rangbuocItem in rangbuocList)
            {

                var monHocRangBuoc = (from m in entities.MonHocs
                                      where m.MonHocID.Equals(rangbuocItem.MonHocIDTruoc)
                                      select m).ToList().FirstOrDefault();
                string strMHRangBuoc = "<b>" + monHocRangBuoc.MonHocID + "</b> - " + monHocRangBuoc.TenMonHoc;
                if (rangbuocItem.LoaiRangBuoc == 0)
                {
                    if (tienQuyetList.IndexOf(strMHRangBuoc) == -1)
                        tienQuyetList += strMHRangBuoc + "<br/>";
                }
                else if (rangbuocItem.LoaiRangBuoc == 1)
                {
                    if (truocList.IndexOf(strMHRangBuoc) == -1)
                        truocList += strMHRangBuoc + "<br/>";
                }
                else if (rangbuocItem.LoaiRangBuoc == 2)
                {
                    if (songHanhList.IndexOf(strMHRangBuoc) == -1)
                        songHanhList += strMHRangBuoc + "<br/>";
                }
            }
            strSongHanh = songHanhList;
            strTienQuyet = tienQuyetList;
            strTruoc = truocList;
            return true;
        }




        public bool LayMonRangBuoc(string monHocID, string nganhID, out List<string> strSongHanh, out List<string> strTienQuyet, out List<string> strTruoc)
        {
            var rangbuocList = from m in entities.RangBuocMons
                               where m.MonHocIDSau.Equals(monHocID)
                               && m.NganhID.Equals(nganhID)
                               select m;
            //0: la mon tien quyet; 1 la mon hoc truoc; 2: mh song hanh
            strSongHanh = new List<string>();
            strTienQuyet = new List<string>();
            strTruoc = new List<string>();
            

            foreach (var rangbuocItem in rangbuocList)
            {

                var monHocRangBuoc = (from m in entities.MonHocs
                                      where m.MonHocID.Equals(rangbuocItem.MonHocIDTruoc)
                                      select m).ToList().FirstOrDefault();
                
                if (rangbuocItem.LoaiRangBuoc == 0) //tien quyet
                {
                    if (strTienQuyet.IndexOf(monHocRangBuoc.MonHocID) == -1)
                        strTienQuyet.Add(monHocRangBuoc.MonHocID);
                }
                else if (rangbuocItem.LoaiRangBuoc == 1) //truoc
                {
                    if (strTruoc.IndexOf(monHocRangBuoc.MonHocID) == -1)
                        strTruoc.Add(monHocRangBuoc.MonHocID);
                }
                else if (rangbuocItem.LoaiRangBuoc == 2) //song hanh
                {
                    if (strSongHanh.IndexOf(monHocRangBuoc.MonHocID) == -1)
                        strSongHanh.Add(monHocRangBuoc.MonHocID);
                }
            }
            
            return true;
        }
    }
}
